<?

/*	Text.search OBJECT
*	Methods:
*	output
*
*	Constructor:
*
*
*					*/


class TextSearch extends modTEXT {

	function __construct( $vars ) {

		$this->_addvars( $vars );
		$this->_ref();
		
	}
	
	public function output() {
		
		
		if( $this->action )
			$nodes = $this->{ "filter_" . $this->action }();
				
		$this->addclass( $this->action );
		
		$result = is_array( $nodes ) ?
			"<form id='{$this->modclass}.". rand() ."'>" . implode("\n", $nodes ) . "</form>\n" . $script : (( $nodes )? $nodes : "empty.");
		
		// push_content puts content after text, parse before
		$this->push_content = $result;
		
		
		return $this->parse() . $this->render_js();
		
	}

	private function filter_dyn() {
		
		return "Search.";
	}
	
	private function filter_extended() {
		global $DB;
		
		if( !$this->vars->query && !( $this->vars->query = $_GET["query"] ))
			return null;
			
		foreach( $this->vars->types as $type )
			
			if( $list = $DB->filter_objects( $type, "active && nameid in(select textid from txtdump where short like ". $DB->smartq( "%{$this->vars->query }%" ) .")" )) {
				
				$result .=  "<br /><h2>" . translate( $type ) . "</h2>";
				foreach( $list as $Obj ) $result .=  "<a href='{$Obj->uri()}'>{$Obj->get( "name" )}</a><br />";	
				
			}
		
		$result = ( $result )?
			"<h1>Resultaten voor <i>\"{$this->vars->query}\"</i></h1>$result" : "Geen resultaten gevonden...";
		
		return $result;
	}
	
	/**
	 * 	filter any type by the alphabet / year
	 *
	 *	starts with the letter 'a'
	 * 	an ajax request returns the type objects by first letter
	 *
	 *	@return 		alphabetic list && object list starting with a
	 */
	
	private function filter_abc() {
		global $DB;
				
		// print alphabetic list with change year / abc
		foreach( array( "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","9","!" ) as $n ) {
			$abc[] = "<li><input class='button " . ((!$abc)? "active'":"'") . "' type='button' value='$n' /></li>";
		}
		$abc[] = "<li class='option'><input class='change' type='button' value='years' /></li>";
		
		
		// print year list with change year / abc
		$year 	= $DB->get( 'cal.single', 'active ORDER BY date ASC', 'date');
		$year 	= substr($year['date'],0,4);
		while ( $year <= date('Y', time())) {
		
			$years[] = "<li><input class='button year' type='button' value='$year' /></li>";
			$year++;
		}
		$years[] = "<li class='option'><input class='change' type='button' value='abc' /></li>";
				
		
		$abc[] = $years[] = "<li class='option'><input class='button' type='button' value='search' /></li>";
		$abc[] = $search[] = $years[] = "<li class='stable'></li>";
		
		// print output
		$lines[] = "<ul class='field abc'>" . implode( "\n", $abc ) . "</ul>";
		$lines[] = "<ul class='field years hide'>" . implode( "\n", $years ) . "</ul>";
		$lines[] = "<ul class='field search hide'>" . implode( "\n", $search ) . "</ul>";
		//$lines[] = "<ul class='field years'>" . implode( "\n", $years ) . "</ul>";
		
		
		if ( $bands = $DB->filter_objects( $this->vars->type, 'active AND SUBSTR( <%lang:nameid%>, 1, 1) = "a" ORDER BY <%lang:nameid%> ASC' ) ) {
			foreach ($bands as $band) {
				$list .= '<li><a href="' . $band->uri() .'" title="' . stripslashes($band->get('name')) . '">' . stripslashes($band->get('name')) . '</a></li>';
			}
		}
		
		$lines[] = "<ul class='results left'>" . $list . "</ul>";
		
		$this->js = "
			mod = new TextSearch( '" . json_encode( $this->vars ) . "' );
			mod.change();
		";
		
		return $lines;
	}
	
	
	private function filter_search() {
		global $DB;
	
		$abc[] = '<input type="text" name="q" />';
		$abc[] = '<input type="submit" onclick="return false;" id="search_button" class="button" value="search" />';
		
		$lines[] = "<ul class='field search'>" . implode( "\n", $abc ) . "</ul>";
		$lines[] = "<ul class='results left'>" . $list . "</ul>";

		$this->js = "
			mod = new TextSearch( '" . json_encode( $this->vars ) . "' );
			mod.search();
		";
		
		return $lines;
	}
	
	
	/**
	 * 	filter any type by the year
	 *
	 *	starts with the current year
	 * 	an ajax request returns the type objects by year
	 *
	 *	@return 		numeric list && object list starting with the current year
	 */
	
	
	function ajax_pull() {
		
		$update = $this->{ "ajax_pull_" . $this->vars->active }();

		return json_encode( $update );
	}
	
	
	/** 
	 * pull abc
	 *
	 * print band list by any given letter
	 * http request & return
	 *
	 * @return		list with linked band names
	 */
	function ajax_pull_abc() {
		global $DB;
				
		// check for value of vars		
		if ((int) $this->vars->value == 9 ) {
			$condt = 'regexp [0-9]';
		} elseif ( $this->vars->value == '!' ) {
			$condt = 'regexp [^A-Za-z0-9]';
		} elseif ((int) $this->vars->value > 1950 ) {
			$list = $this->ajax_pull_years();
		} elseif ( $this->vars->value == 'search' ) {
			$search = $this->ajax_pull_search();
		} else {
			$condt = '= "' . mysql_real_escape_string($this->vars->value) . '"';
		}
		
		
		// get bands
		if ( !empty($condt) && $bands = $DB->filter_objects( json_decode($this->vars->vars)->type, 'active AND SUBSTR( <%lang:nameid%>, 1, 1) ' . $condt . ' ORDER BY <%lang:nameid%> ASC' )) {
			foreach ($bands as $band) {
				$list[] = array( "name"=> $band->get('name'), "uri"=>$band->uri() );
			}
		}
		
		if ( $list )
			return json_encode($list);
		else if( $search )
			return implode( "\n", $search );
		else 
			return '<li>empty.</li>';
	}
	
	
	/** 
	 * pull years
	 *
	 * print band list by any given year
	 * http request & return
	 *
	 * @return		listed dates with linked band names
	 */
	function ajax_pull_years() {
		global $DB;
		
		// setup new calendar and filter events in that year
		$CAL = new CALENDAR();
		
		if ( $nodes = $CAL->filter_nodes( $DB->smartq($this->vars->value ) . '-01-01', 365, "cal.single", json_decode($this->vars->vars)->type, false, 512 )) {
			foreach ($nodes as $node) {
			
				if ( $node->obj && $node->obj->get('name') ) {
					$name = html_entity_decode(htmlspecialchars($node->obj->get('name'))); 
					$date = date('d.m', strtotime($node->get('date')));
			
					$list[] = array( "date"=> $date, "name" => $name, "uri" => $node->obj->uri() );
				}
			}
		}
	
		if ( $list )
			return $list;
		else 
			return '<li>empty.</li>';
	
	}
	
	
	/**
	 * pull search engine
	 * print search field / results if there was a search
	 * @return		list of related objects (defined by types in vars)
	 */
	function ajax_pull_search() {
		
		$list[] = '<input type="text" name="q" />';
		$list[] = '<input type="submit" onclick="return false;" id="search_button" class="button" value="search" />';
		
		return $list;
	
	}
	

	function ajax_pull_searcher() {
		global $DB;
		
		if( strlen( $this->vars->value ) > 2 ) {
		
			$type = json_decode( $this->vars->vars )->type;
		
			$objects = $DB->filter_objects( $type, 'active ORDER BY <%lang:nameid%> ASC' );
			foreach( $objects as $object ) {
		
				if( stristr( $object->get('name'), $this->vars->value )) 
					$list[] = array( "name"=> $object->get('name'), "uri"=> $object->uri() );
			}
		
			if ( $list )
				return json_encode($list);
			else 
				return '<li>empty.</li>';
		
		} else {
		
			return '<li>' . translate( 'Maak uw term langer' ) . '</li>';
		}
		
		
	}
	

} 

?>